Skip to content

fix(git): add missing argument injection guards#3545

Open
ElliotJLT wants to merge 1 commit intomodelcontextprotocol:mainfrom
ElliotJLT:fix/git-argument-injection-guards
Open

fix(git): add missing argument injection guards#3545
ElliotJLT wants to merge 1 commit intomodelcontextprotocol:mainfrom
ElliotJLT:fix/git-argument-injection-guards

Conversation

@ElliotJLT
Copy link

Summary

  • git_diff and git_checkout already reject user inputs starting with - to block flag injection attacks — but four other functions lacked the same protection: git_show, git_create_branch, git_log (timestamps), and git_branch (contains/not_contains)
  • This PR adds consistent defense-in-depth guards to all four, matching the existing pattern and comment style
  • Adds 6 new test cases covering each unguarded parameter

Context

An attacker with filesystem write access (e.g. via mcp-filesystem) could create malicious git refs like --format=evil under .git/refs/heads/. Without the leading-dash check, these values would be passed as flags to git commands via GitPython. The existing guards on git_diff and git_checkout (+ tests for malicious refs) demonstrate this threat model is already recognised — this PR closes the remaining gaps.

Test plan

  • All 41 tests pass (35 existing + 6 new)
  • New tests verify rejection of flag-like inputs for git_show, git_create_branch, git_log, and git_branch
  • New test verifies git_show rejects malicious refs even when they exist on disk
  • Existing tests confirm no regressions in normal operation

🤖 Generated with Claude Code

…te_branch, git_log, and git_branch

git_diff and git_checkout already reject user-supplied values starting
with '-' to prevent flag injection (even when a malicious ref exists via
filesystem manipulation). The same defense-in-depth pattern was missing
from four other functions:

- git_show: revision parameter passed directly to repo.commit()
- git_create_branch: branch_name and base_branch unchecked
- git_log: start_timestamp and end_timestamp passed to --since/--until
- git_branch: contains and not_contains passed as raw args to repo.git.branch()

Adds the same startswith("-") guards with matching tests for each function.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant